package com.ruoyi.system.service.impl;

import java.util.List;

//import org.apache.oro.text.perl.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;

/**
 * 公告 服务层实现
 * 
 * @author ruoyi
 * @date 2018-06-25
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        Long lastId = noticeMapper.selectLastNotice();
        SysNotice lastNotice = noticeMapper.selectNoticeById(lastId);
        System.out.println(lastId);
        String lastTitle = lastNotice.getNoticeTitle();
        System.out.println(lastTitle);
        lastTitle = StringUtils.substring(lastTitle,4);
        System.out.println(lastTitle);
//        (lastTitle,4)
        lastNotice.setNoticeTitle(lastTitle);
        noticeMapper.updateNotice(lastNotice);
        String content = notice.getNoticeContent();
//        String newcontent = content.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("]*>", "\n\t");
        // <p>段落替换为换行
        content = content.replaceAll("<p .*?>", "\r\n");
        // <br><br/>替换为换行
        content = content.replaceAll("<br\\s*/?>", "\r\n");
        // 去掉其它的<>之间的东西
        content = content.replaceAll("\\<.*?>", "");
        // 去掉空格
        content = content.replaceAll("&nbsp;", " ");
        // 还原HTML
        // content = HTMLDecoder.decode(content);
        notice.setNoticeContent(content);
        String title = notice.getNoticeTitle();
        title = "(最新)" + title;
        notice.setNoticeTitle(title);
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        String content = notice.getNoticeContent();
//        String newcontent = content.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("]*>", "\n\t");
        // <p>段落替换为换行
        content = content.replaceAll("<p .*?>", "\r\n");
        // <br><br/>替换为换行
        content = content.replaceAll("<br\\s*/?>", "\r\n");
        // 去掉其它的<>之间的东西
        content = content.replaceAll("\\<.*?>", "");
        // 去掉空格
        content = content.replaceAll("&nbsp;", " ");
        notice.setNoticeContent(content);
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(String ids)
    {
        return noticeMapper.deleteNoticeByIds(Convert.toStrArray(ids));
    }
}
